//
//  UIImageView+Extension.swift
//  CosClub
//
//  Created by 沙睿 on 2020/4/21.
//  Copyright © 2020 ios. All rights reserved.
//

import UIKit
import Kingfisher

extension UIImageView {
    public func url_image(_ url : String?, placeholder: UIImage? = nil)
    {
        self.image = placeholder
        self.kf.indicatorType = .activity
        guard let trueUrl = url  else {print("无地址", url ?? ""); return }
        guard let nurl = URL(string: trueUrl)  else {print("无图片", url ?? ""); return }
        //        let option : KingfisherOptionsInfo = [
        //            .backgroundDecode,  //后台解码 gif
        //            .onlyLoadFirstFrame, //gif 只读第一帧
        ////            .fromMemoryCacheOrRefresh  //如果设置，Kingfisher会尝试从内存缓存中首先检索图像。 如果图像不在内存缓存中，则会忽略磁盘缓存，但是会再次从网络下载图像
        //            ]
        
        self.kf.setImage(with: nurl, placeholder: placeholder)
    }
}

extension UIButton {
    public func url_image(_ url : String?, placeholder: UIImage? = nil)
    {
        self.setImage(placeholder, for: .normal)
        
        guard let trueUrl = url  else {
            print("无地址", url ?? "");
            return
        }
        
        guard let nurl = URL(string: trueUrl)  else {
            print("无图片", url ?? ""); return
        }

        self.kf.setBackgroundImage(with: nurl, for: .normal, placeholder: placeholder)
    }
}
